package ua.com.abakumov.android.prostopleer.engines

import ua.com.abakumov.android.prostopleer.util.Composition
import java.util.{List => JList}

/**
 * Engine

 * @author alexander-abakumov
 *         Date: 25.10.12
 */
abstract class Engine {

  protected var protocol: String = ""
  protected var host: String = ""
  protected var port: String = ""
  protected var path: String = ""
  protected var downloadPath: String = ""
  protected var query: String = ""
  protected var downloadQuery: String = ""
  protected var fragment: String = ""

  type MessageListener = String => Unit
  type Listener = Unit => Unit

  private def defMessageListener: MessageListener = {
    def defFn(x: String): MessageListener = (x => {
      // Do nothing with x
    })
    defFn("")
  }

  private def defListener: Listener = {
    def f(u: Unit): Listener = u => {
      // Do nothing
    }
    f()
  }

  protected var compositionSavedListener: MessageListener = defMessageListener
  protected var startedDownloadingListener: MessageListener = defMessageListener
  protected var mountCardErrorListener: Listener = defListener

  def setListeners(compositionSavedListener: MessageListener,
                   startedDownloadingListener: MessageListener,
                   mountCardErrorListener: Listener) {

    this.compositionSavedListener = compositionSavedListener
    this.startedDownloadingListener = startedDownloadingListener
    this.mountCardErrorListener = mountCardErrorListener
  }

  def search(request: String): JList[Composition] = {
    parse(mine(request))
  }

  protected def mine(request: String): String

  protected def parse(rawResult: String): JList[Composition]

  def download(composition: Composition)

  def getProtocol = protocol

  def getHost = host

  def getDownloadPath = downloadPath

  def getDownloadQuery = downloadQuery
}